package cn.drew.org.service.impl;

import cn.drew.basic.utils.PageList;
import cn.drew.org.domain.Employee;
import cn.drew.org.mapper.EmployeeMapper;
import cn.drew.org.query.EmployeeQuery;
import cn.drew.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void add(Employee employee) {
        employeeMapper.add(employee);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }



    @Override
    public PageList<Employee> queryByPage(EmployeeQuery employeeQuery) {
        Integer count = employeeMapper.queryByCount(employeeQuery);
        if (count == 0 || count == null){
            return new PageList(0,new ArrayList());
        }else{
            List<Employee> employees = employeeMapper.queryByPage(employeeQuery);
            return new PageList<Employee>(count,employees);
        }

    }

    @Override
    public void patchDelete(List<Long> ids) {
        employeeMapper.patchDelete(ids);
    }

    @Override
    public List<Employee> findByDeptid(Long deptid) {
        List<Employee> employees = employeeMapper.findAll();
        System.out.println(deptid);
        //将departmentId 和deptid 相等的数据过滤 ,再转化为list
        return employees.stream().filter(emp -> deptid.equals(emp.getDepartmentId())).collect(Collectors.toList());

    }
}
